package com.yuri.news.controller;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuri.news.domain.Result;
import com.yuri.news.entity.Privilege;
import com.yuri.news.entity.PrivilegePack;
import com.yuri.news.service.PrivilegeService;

/**
 * 权限控制器
 * @author Yuri
 *
 */
//允许跨域访问
@CrossOrigin
// json形式返回
@ResponseBody
// 控制器
@Controller
public class PrivilegeController {
	// 日志打印
	private final Logger logger = LoggerFactory.getLogger(PrivilegeController.class);
	// 依赖注入
	@Autowired
	PrivilegeService privilegeService;

	/**
	 * 添加权限
	 * @param privilege
	 */
	@RequestMapping(value = "addPri", method = RequestMethod.POST)
	public Result<Privilege> addPri(Privilege privilege) {
		// 日志打印
		logger.info("\n 添加权限(addPri) Privilege = " + privilege.toString());
		Result<Privilege> result = new Result<>();
		try {
			if(privilegeService.savePrivilege(privilege)) {
				result.setSuccess("添加成功！");
			}else {
				result.setError("此权限已经存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("添加出现异常！");
		}
		logger.info("\n 添加权限(addPri)结束  result = " + result.toString());
		return result;
	}

	/**
	 * 删除权限
	 * 
	 * @param privilege
	 */
	@RequestMapping(value = "delPri", method = RequestMethod.DELETE)
	public Result<Privilege> delPri(Privilege privilege) {
		// 日志打印
		logger.info("\n 删除权限(delPri) Privilege = " + privilege.toString());
		Result<Privilege> result = new Result<>();
		
		try {
			if(privilegeService.deletePrivilege(privilege)) {
				result.setSuccess("删除成功！");
			}else {
				result.setError("不存在此条权限信息");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("删除发生异常错误！");
		}
		// 日志打印
		logger.info("\n 删除权限(delPri)结束 result = " + result.toString());
		return result;
	}

	/**
	 * 根据id修改权限是否可用
	 * 
	 * @param id
	 */
	@RequestMapping(value = "setPO", method = RequestMethod.PUT)
	public Result<Privilege> setPrivilegeOperation(Long id) {
		// 日志打印
		logger.info("\n 修改权限是否可用(setPO)开始 Privilege.id = " + id);
		Result<Privilege> result = new Result<>();
		try {
			if(privilegeService.setPrivilegeOperation(id)) {
				result.setSuccess("修改成功！");
			}else {
				result.setError("不存在此条权限！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		return result;
	}

	/**
	 * 查询所有权限信息 每个
	 * @param isAva		是否可用
	 * @param page		当前页
	 * @param size		每页显示数据
	 * @return 每个PrivilegePack包装了单个（用户/角色/部门）的 所有 信息
	 */
	@SuppressWarnings("deprecation")
	@RequestMapping(value = "findAuthorAll", method = RequestMethod.GET)
	public Result<PrivilegePack> findAuthorAll(
			@RequestParam(name = "isAva", defaultValue = "true") Boolean isAva,
			@RequestParam(name = "page",defaultValue="0") Integer page,
			@RequestParam(name = "size", defaultValue="1000") Integer size) {
		
		logger.info("\n 查询所有权限(findAuthorAll) 参数isAva = " + isAva + "\t page = " + page + "\t size = " + size);
		Result<PrivilegePack> result = new Result<>();
		try {
			// 分页信息
			Order order 	= new Order(Direction.ASC, "id"); // 根据字段id进行升序排序
			PageRequest pageRequest = PageRequest.of(page, size, new Sort(order));// 分页排序查询
			
			List<PrivilegePack> privilegePacks = privilegeService.listAuthorAll(isAva, pageRequest);
			if(privilegePacks != null && privilegePacks.size() > 0) {
				result.setData(privilegePacks);
				result.setSuccess("查询成功");
			}else {
				result.setError("暂无权限信息！请稍后再试");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		
		logger.info("\n 查询所有权限(findAuthorAll) 结果5 result = " + result);
		return result;
	}
}
